/*
 * cryptomfs - a macfuse implementation of cryptofs.
 * Copyright (C) 2008  Greg J. Hedlund
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#import "NewFilesystemController.h"
#import "CustomIconView.h"
#import "FSOps.h"

@implementation NewFilesystemController (private)

-(void)createAndMount:(NSArray*)args
{
	NSAutoreleasePool * autoReleasePool = [[NSAutoreleasePool alloc] init];
	
	NSNumber * copyIconObj = (NSNumber*)[args objectAtIndex:3];
	BOOL copyIcon = [copyIconObj boolValue];
	
	if( [FSOps createFilesystemAtPath:[args objectAtIndex:0]
						  volumename:[args objectAtIndex:1]
						  volumeicon:[args objectAtIndex:2]
							copyicon:copyIcon] )
	{
		[FSOps mountFilesystemAtPath:[args objectAtIndex:0]];
		
		NSUserDefaultsController * prefs = [NSUserDefaultsController sharedUserDefaultsController];
		id someVal = [[prefs values] valueForKey:@"closeAfterMount"];
		if(someVal != nil) {
			NSNumber * numVal = (NSNumber*)someVal;
			BOOL closeApp = [numVal boolValue];
			if(closeApp) {
				[[NSApplication sharedApplication] terminate:self];
			}
		}
	}
	
	[autoReleasePool release];
}

-(void)createSheetDidEnd:(NSSavePanel*)panel returnCode:(int)returnCode contextInfo:(void*)contextInfo
{
	if(returnCode == NSOKButton) {
		NSString * selectedFile = [panel filename];
		if(selectedFile != nil) 
		{
			NSArray * args = [NSArray arrayWithObjects:[selectedFile copy],
														[[m_volumeNameField stringValue] copy],
														[[m_iconView customIconPath] copy],
							   [[NSNumber numberWithBool:([m_copyIconBox state] == NSOnState)] copy], nil];
			
			[NSThread detachNewThreadSelector:@selector(createAndMount:) toTarget:self withObject:args];
			[[self window] orderOut:self];
		}
		[self release];
	}
}

-(void)browseIconSheetDidEnd:(NSOpenPanel*)panel returnCode:(int)returnCode contextInfo:(void*)contextInfo
{
	if(returnCode == NSOKButton) {
		NSString * selectedFile = [panel filename];
		if(selectedFile != nil) {
			[m_iconView setCustomIconPath:selectedFile];
		}
	}
}

@end

@implementation NewFilesystemController

// NSWindowController overrides
-(void)awakeFromNib
{
	// setup UI
	NSImage * defaultIcon = [[[NSImage alloc] initWithContentsOfFile:[m_iconView customIconPath]] autorelease];
	if(defaultIcon != nil) {
		[m_iconView setImage:defaultIcon];
	}
	
	[m_volumeNameField setStringValue:@"My Cryptomfs Volume"];
}

// button actions
-(IBAction)createClicked:(id)sender
{
	// display the save file dialog...
	NSSavePanel * savePanel = [NSSavePanel savePanel];
	[savePanel setCanCreateDirectories:YES];
	[savePanel setAllowedFileTypes:[NSArray arrayWithObject:@"cryptofs"]];
	NSString * filenameString = 
		[NSString stringWithFormat:@"%@.%@", [m_volumeNameField stringValue], @"cryptofs"];
	[savePanel beginSheetForDirectory:nil file:filenameString modalForWindow:[self window]
						modalDelegate:self 
					   didEndSelector:@selector(createSheetDidEnd:returnCode:contextInfo:)
						  contextInfo:nil];
}

-(IBAction)browseIconClicked:(id)sender
{
	// display the open file dialog...
	NSOpenPanel * openPanel = [NSOpenPanel openPanel];
	[openPanel setCanCreateDirectories:YES];
	[openPanel setCanChooseDirectories:NO];
	[openPanel setAllowedFileTypes:[NSArray arrayWithObject:@"cryptofs"]];
	[openPanel beginSheetForDirectory:nil file:nil modalForWindow:[self window] 
						modalDelegate:self 
					   didEndSelector:@selector(browseIconSheetDidEnd:returnCode:contextInfo:) 
						  contextInfo:nil];
}

@end
